package com.wsc.wisdomchina.activitity.introduction;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.JsonTools;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.activitity.baseactivity.BaseFragmentActivity;
import com.wsc.wisdomchina.activitity.introduction.fragment.CommunityInformationFragment;
import com.wsc.wisdomchina.activitity.introduction.fragment.LeaderListFragment;
import com.wsc.wisdomchina.activitity.introduction.fragment.OrganizationListFragment;
import com.wsc.wisdomchina.activitity.introduction.fragment.StreetIntroduceFragment;
import com.wsc.wisdomchina.activitity.introduction.fragment.TownFragment;
import com.wsc.wisdomchina.application.MyApplication;
import com.wsc.wisdomchina.callback.ContributorView;
import com.wsc.wisdomchina.fragment.EightModuleFragment;
import com.wsc.wisdomchina.javaBean.AroundTitleListBean;
import com.wsc.wisdomchina.javaBean.RespObj;
import com.wsc.wisdomchina.presenter.ContributorPresenter;
import com.wsc.wisdomchina.view.PagerSlidingTabStrip;

import java.util.ArrayList;
import java.util.List;

/**
 * 简介
 */
public class BriefIntroductionActivity extends BaseFragmentActivity implements View.OnClickListener, ContributorView {
    private ImageView regist_back;
    private TextView tv_headtitle;
    private ViewPager viewPager;
    private PagerSlidingTabStrip tabs;// 新闻栏滑动条
    private Context context;
    private MyApplication myApplication;
    private List<String> nameList;
    private List<Integer> typeList;
    List<Fragment> lastlistfragmentView;
    private String name;
    private String cidNumber;
    private ContributorPresenter contributorPresenter;
    private final String FINDALLLISTFLAG = "FINDALLLISTFLAG";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.peripheralsearch_layout);
        context = this;
        myApplication = (MyApplication) getApplication();
        name = getIntent().getStringExtra("name");
        cidNumber = getIntent().getStringExtra("id");
//        Log.e("Liu", "onCreate:  name=" + name + "  cidNumber=" + cidNumber);
        contributorPresenter = new ContributorPresenter(this, context);
        showData();
        initView();
    }

    private void initView() {
        regist_back = (ImageView) findViewById(R.id.regist_back);
        regist_back.setOnClickListener(this);
        tv_headtitle = (TextView) findViewById(R.id.tv_headtitle);
        tv_headtitle.setText(name);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        tabs.setIndicatorHeight(Tools.dip2px(context, 3));
        tabs.setTextSize(Tools.dip2px(context, 17));
        tabs.setTextColor(getResources().getColor(R.color.text_color_one));
        tabs.setSelectedTextColor(getResources().getColor(R.color.little_white));

        if (nameList != null && typeList != null && nameList.size() > 0 && typeList.size() > 0) {
            showTabsViewPager();
        }
    }

    private void showData() {
        if (nameList == null && typeList == null)
            contributorPresenter.getAroundFindTitleList(cidNumber, FINDALLLISTFLAG);
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            if (flag.equals(FINDALLLISTFLAG)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                if (code.equals("200")) {
                    String message = JsonTools.objectToJsonString(respObj.getMessage());


                    List<AroundTitleListBean> listData = JsonTools.jsonStringToList(message, AroundTitleListBean.class);
                    /**
                     * 添加假数据
                     */
                    nameList = new ArrayList<>();
                    typeList = new ArrayList<>();
                    nameList.add("乡镇特色");
                    nameList.add("街情介绍");
                    nameList.add("领导介绍");
                    nameList.add("机构设置");
                    nameList.add("社区信息");
                    typeList.add(1);
                    typeList.add(2);
                    typeList.add(3);
                    typeList.add(4);
                    typeList.add(5);
//                    if (listData != null && listData.size() > 0) {
//                        nameList = new ArrayList<>();
//                        typeList = new ArrayList<>();

                    for (AroundTitleListBean lookAllListBean : listData) {
//                        nameList.add(lookAllListBean.getTitle());
//                        typeList.add(lookAllListBean.getType());
                    }
                    if (nameList.size() > 0 && typeList.size() > 0) {
//                        myApplication.setTitleListType(typeList);
//                        myApplication.setTitleListName(nameList);
                        showTabsViewPager();
                    }
                }
//                }

            }

        } catch (Exception e) {

        }
    }

    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error, String flag) {

    }


    private void showTabsViewPager() {
        lastlistfragmentView = new ArrayList<>();
        List<String> lastEightModuleFragment = new ArrayList<>();
        for (Integer type : typeList) {
            Bundle bundle = new Bundle();
            bundle.putInt("type", type);
            bundle.putString("cid", cidNumber);

            Fragment fragment;
            if (type == 2) {
                fragment = StreetIntroduceFragment.newInstance(bundle);
            } else if (type == 5) {
                bundle.putString("id", "9861135247");
                fragment = CommunityInformationFragment.newInstance(bundle);
            } else if (type == 3) {
                bundle.putString("id", "9861135247");
                fragment = LeaderListFragment.newInstance(bundle);
            } else if (type == 4) {
                bundle.putString("id", "9861135247");
                fragment = OrganizationListFragment.newInstance(bundle);
            } else {
                bundle.putString("id", "9861135247");
                bundle.putString("name", "乡镇特色");
                bundle.putString("idmodule", "5240197863");
                fragment = TownFragment.newInstance(bundle);
            }

            lastlistfragmentView.add(fragment);
            lastEightModuleFragment.add(EightModuleFragment.class.getSimpleName() + type);
        }
        myApplication.setInformationFragmentSelected(lastEightModuleFragment.get(0));
        viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(), lastlistfragmentView, nameList));// 设置viewpageradapter
        tabs.setViewPager(viewPager, lastlistfragmentView, lastEightModuleFragment, this);
    }

    class ViewPagerAdapter extends FragmentStatePagerAdapter {

        private List<Fragment> listfragmentView;// viewList 集合
        private List<String> TITLES;

        public ViewPagerAdapter(FragmentManager fm, List<Fragment> listfragmentView, List<String> TITLES) {
            super(fm);
            this.listfragmentView = listfragmentView;
            this.TITLES = TITLES;
            // TODO Auto-generated constructor stub
        }

        @Override
        public CharSequence getPageTitle(int position) {
            // TODO Auto-generated method stub
            return TITLES.get(position);
        }

        @Override
        public Fragment getItem(int position) {
            // TODO Auto-generated method stub
            return listfragmentView.get(position);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return TITLES.size();
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // TODO Auto-generated method stub

            Fragment ff = (Fragment) super.instantiateItem(container, position);
            ff.setTargetFragment(listfragmentView.get(position), position);

            return ff;
        }

        @Override
        public void destroyItem(View container, int position, Object object) {
            // TODO Auto-generated method stub
            super.destroyItem(container, position, object);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.regist_back:
                finish();
                break;
        }
    }
}
